package com.gbx.ximalayating.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gbx.ximalayating.R;
import com.gbx.ximalayating.entity.CCEntity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by wf on 2016/12/2.
 */

public class ClassifyAdapter extends RecyclerView.Adapter<ClassifyAdapter.ViewHolder> implements View.OnClickListener {
    private final Context context;
    private List<CCEntity> list;
    private ViewHolder holder;
    private RecyclerView recyclerView;
    private OnChildClickListener listener;


    public ClassifyAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    public void setListener(OnChildClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recyclerView = null;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.item_include, parent, false);
        itemView.findViewById(R.id.horizontal_item).setOnClickListener(this);
        holder = new ViewHolder(itemView);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.subTV.setText(list.get(position).getTitle());
        Picasso.with(context).load(list.get(position).getCoverPath()).into(holder.subImg);
    }

    public void addAll(Collection<? extends CCEntity> collection) {
        int size = list.size();
        list.addAll(collection);
        notifyItemRangeInserted(size, collection.size());

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View v) {
        if (recyclerView != null && listener != null) {
            int position = recyclerView.getChildAdapterPosition(v.findViewById(R.id.sub_include));
            listener.onChildClick(position, list);

        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {


        private final CardView include;
        private final View view;
        private final ImageView subImg;
        private final TextView subTV;
        public ViewHolder(View itemView) {
            super(itemView);
            include = ((CardView) itemView.findViewById(R.id.sub_include));
            view = ((View) itemView.findViewById(R.id.horizontal_item));
            subImg = ((ImageView) view.findViewById(R.id.s_include_img));
            subTV = ((TextView) view.findViewById(R.id.s_include_tv));
        }


    }

    public interface OnChildClickListener {
        void onChildClick(int position,List<CCEntity> ccEntities );
    }
}
